package org.example.dingtalkintegrationservice.web;

import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
//import org.example.dingtalkintegrationservice.exception.BodyMissingException;
import org.example.dingtalkintegrationservice.exception.GroovyExecutionException;
import org.example.dingtalkintegrationservice.exception.ResourceNotFoundException;
import org.example.dingtalkintegrationservice.pojo.log.HttpDetail;
import org.example.dingtalkintegrationservice.repository.HttpDetailRepository;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.context.request.WebRequest;


import java.util.Arrays;

/**
 * @author pc
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    @Resource
    HttpDetailRepository httpDetailRepository;
    @ExceptionHandler(ResourceNotFoundException.class)
    public ResponseEntity<Object> handleResourceNotFoundException(ResourceNotFoundException ex, WebRequest request) {
        log.error(ex.getMessage());
        return new ResponseEntity<>(ex.getMessage(), HttpStatus.NOT_FOUND);
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    public ResponseEntity<HttpDetail> handleHttpMessageNotReadableException(
            HttpMessageNotReadableException ex, WebRequest request) {
        HttpDetail errorDetails = getMap(request,ex.getMessage(),ex);
        errorDetails.setTip("请求缺失body");
//        errorDetails.put("timestamp", System.currentTimeMillis());
//        errorDetails.put("status", HttpStatus.BAD_REQUEST.value());
//        errorDetails.put("error", "Bad Request");
//        errorDetails.put("message", "Required request body is missing or incorrectly formatted");
//        errorDetails.put("path", request.getDescription(false));
        httpDetailRepository.save(errorDetails);
        return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
    }
    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseEntity<HttpDetail> handleMissingServletRequestParameterException(
            MissingServletRequestParameterException ex,WebRequest request) {
        HttpDetail httpDetail=getMap(request, ex.getMessage(), ex);
        httpDetail.setTip("请求缺少必要的参数");
        httpDetailRepository.save(httpDetail);
        return new ResponseEntity<>(httpDetail,HttpStatus.BAD_REQUEST);
    }
    @ExceptionHandler(GroovyExecutionException.class)
    public ResponseEntity<HttpDetail> handleExecutionException(GroovyExecutionException ex, WebRequest request){
        HttpDetail httpDetail=getMap(request, ex.getMessage(), ex);
        httpDetail.setTip("请求执行遇到问题");
        httpDetail.setErrorLocation(ex.errorLocation);
        httpDetail.setErrorMethod(ex.errorMethod);
        httpDetailRepository.save(httpDetail);
        return new ResponseEntity<>(httpDetail,HttpStatus.BAD_REQUEST);
    }
    @ExceptionHandler(Exception.class)
    public ResponseEntity<HttpDetail> handleException(Exception ex, WebRequest request){
        HttpDetail httpDetail=getMap(request, ex.getMessage(), ex);
        httpDetail.setTip("系统内部错误");
//        log.error(ex.getMetaClass().toString());
        httpDetailRepository.save(httpDetail);
        return new ResponseEntity<>(httpDetail,HttpStatus.INTERNAL_SERVER_ERROR);
    }

    private HttpDetail getMap(WebRequest request, String message, Exception ex) {
        HttpDetail httpDetail=new HttpDetail();
        httpDetail.setStatus(HttpStatus.BAD_REQUEST.value());
        httpDetail.setMsg(message);
        httpDetail.setPath(request.getDescription(false));
        httpDetail.setErrorClass(ex.getClass().toString());
        httpDetail.setErrorCause(Arrays.toString(ex.getStackTrace()));
        return httpDetail;
    }
}